package Easy;

public class Solution13 {
    // 13.罗马数字转整数
    /*
    *  字符           数值
        I             1
        V             5
        X             10
        L             50
        C             100
        D             500
        M             1000
    * */
    public static int romanToInt(String s) {
        int sum = 0;
        char[] str = s.toCharArray();
        for (int i = 0; i < str.length; ) {
            switch (str[i]) {
                case 'I':
                    try {
                        switch (str[i + 1]) {
                            case 'V':
                                sum += 4;
                                i += 2;
                                break;
                            case 'X':
                                sum += 9;
                                i += 2;
                                break;
                            default:
                                sum += 1;
                                i += 1;
                        }
                    } catch (Exception e) {
                        sum += 1;
                        i += 1;
                    } finally {
                        break;
                    }

                case 'X':
                    try {
                        switch (str[i + 1]) {
                            case 'L':
                                sum += 40;
                                i += 2;
                                break;
                            case 'C':
                                sum += 90;
                                i += 2;
                                break;
                            default:
                                sum += 10;
                                i += 1;
                        }
                    } catch (Exception e) {
                        sum += 10;
                        i += 1;
                    } finally {
                        break;
                    }
                case 'C':
                    try {
                        switch (str[i + 1]) {
                            case 'D':
                                sum += 400;
                                i += 2;
                                break;
                            case 'M':
                                sum += 900;
                                i += 2;
                                break;
                            default:
                                sum += 100;
                                i += 1;
                        }
                    } catch (Exception e) {
                        sum += 100;
                        i += 1;
                    } finally {
                        break;
                    }
                case 'V':
                    sum += 5;
                    i += 1;
                    break;
                case 'L':
                    sum += 50;
                    i += 1;
                    break;
                case 'D':
                    sum += 500;
                    i += 1;
                    break;
                case 'M':
                    sum += 1000;
                    i += 1;
                    break;
                default:
                    System.out.println("你输入的不是罗马字符");
            }
        }

        return sum;
    }

    public static void main(String[] args) {
        System.out.println(romanToInt("III"));
    }


}
